<?php
class UserController extends commonController{
	
	public function userIndex(){
		
	}
	/*
	 *登录 
	 */
	public function login(){
		if($_POST){
			$username = $_POST['username'];
			$passwd = md5($_POST['passwd']);
			$user_value = M('hd_user')->where("username = '{$username}'")->find();
			if(!empty($user_value)){
				if($user_value['username'] == $username && $user_value['passwd'] == $passwd ){
					$_SESSION['username'] = $username;
					$_SESSION['uid'] = $user_value['uid'];
					
					if(isset($_POST['auto'])){
							setcookie(session_name(),session_id(),time()+33600*24*7,'/');
					}else{
							setcookie(session_name(),session_id(),0,'/');
					}
					$this->success('登录成功','./');
				}else{
					$this->error('用户名或密码错误输入错误');
				}
			}else{
				$this->error('用户名不存在');
			}
		}
	}
	/*
	 * 注册
	 */
	public function register(){
		if(IS_POST){
			$username = $_POST['username'];
			$passwd = md5($_POST['passwd']);
			$user_value = M('hd_user')->where("username = '{$username}'")->find();
			if(empty($user_value)){
				$data = array(
				'username' => $_POST['username'],
				'passwd' => md5($_POST['passwd']),
				'point' => '20',
				'restime'=>time(),
				'logintime'=>time(),
				'loginip' =>$_SERVER['SERVER_ADDR'],
				);
				M('hd_user')->add($data);
				$uid = M('hd_user')->where("username = '{$username}'")->find();
					$_SESSION['username'] = $username;
					$_SESSION['uid'] = $uid['uid'];
				setcookie(session_name(),session_id(),0,'/');
				$this->success('注册成功',__APP__);
			}else{
				$this->error('用户名已存在');
			}	
		}
	}
	/*
	 *验证查询异步  验证用户名是否存在
	 */
	public function check_username(){
		if($_POST){
			$username = $_POST['username'];
			$user_value = M('hd_user')->where("username = '{$username}'")->find();
			if(!empty($user_value)){
				echo 0;
			}else{
				echo 1;
			}
		}
	}
	/*
	 *验证用户密码 异步  ——————————————————————未完
	 */
	public function check_userpassword(){
//		$userInfo= current($this->userInfo());//
////		var_dump($userInfo);die;
//		$uid  = $userInfo['uid'];
//		echo $uid;
//		$probability =	$this->probability($uid);
//		var_dump($probability);die;
		if($_POST){
			$username = $_POST['username'];
			$passwd = md5($_POST['pwd']);
			$user_value = M('hd_user')->where("username = '{$username}'")->find();
			if($user_value['username'] == $username && $user_value['passwd'] == $passwd ){
					$_SESSION['username'] = $username;//修改前
					$_SESSION['uid'] = $user_value['uid'];
					//写入用户信息 到session
				$userInfo= $this->userInfo();//
				$userInfoKeys = array_keys(current($userInfo));
				$userInfoValues = array_values(current($userInfo));
				foreach ($userInfoKeys as $k => $v){
					$_SESSION["$userInfoKeys[$k]"] = $userInfoValues[$k];
				};
					if(isset($_POST['auto'])){
							setcookie(session_name(),session_id(),time()+33600*24*7,'/');
					}else{
							setcookie(session_name(),session_id(),0,'/');
					}				
						$user_value['passwd'] ='';//将密码字段清空
						//计算采纳率 追加返回值

						$uidnum = $user_value['uid'];
				
				//计算采纳率
			if($user_value['answer'] == 0 || $user_value['accept']==0 ){
				$probability='0%';
				
			}else{
				$probability = $user_value['accept']/$user_value['answer']*100;
				$probability =round($probability,2).'%';
				
			}
					$user_value['probability'] = $probability;
					echo json_encode($user_value);
					//记录用户登录时间
					M('hd_user')->exec("UPDATE hd_user SET logintime='{time()}',loginip='{$_SERVER['SERVER_ADDR']}' WHERE username='{$username}'");
					
				}else{
					echo 0;
			}
		}
	}
	/*
	 * 验证码异步
	 */
	public function check_code(){
		if($_POST){
			if(strtoupper($_POST['verify']) == $_SESSION['code'] ){
				echo 1;
			}else{
				echo 0;
			}
		}
	}
	/*
	 * 退出
	 */
	public function loginout(){
		session_unset();
		session_destroy();		
		$this->success('注销成功','./');
	}
	public function code(){
		$code =new Code();
		$code->ImgCodeShow();
	}

}

?>